import { GameSocket } from "./engine/GameSocket";
import { Logger } from "./engine/Logger";
import { RemoteAssetManager } from "./engine/RemoteAssetManager";
import UIManager from "./engine/UIManager";
import { UI_Config } from "./engine/UI_Config";
import { GameConfig } from "./game/config/GameConfig";
import { RoomConfig } from "./game/config/RoomConfig";
import { ViewConfig } from "./game/config/ViewConfig";
import { AccountModel } from "./game/model/AccountModel";
import { GameResult } from "./game/model/GameResult";
import { RoomModel } from "./game/model/RoomModel";
import { RoomPlayer } from "./game/model/RoomPlayer";
import SettingModel from "./game/model/SettingModel";
import { UserModel } from "./game/model/UserModel";
import { VideoInfo } from "./game/model/VideoInfo";
import { UIHelper } from "./game/util/UIHelper";
import Main from "./Main";
import { ServerConfig } from "./net/ServerConfig";
import { SocketClient } from "./net/SocketClient";
import { NativeSdkHelper } from "./sdk/NativeSdkHelper";

export default class GameController {

    public static instance: GameController = new GameController();
    private dianjiSpine: sp.Skeleton;

    public init(main: Main) {

        this.dianjiSpine = main.node.getChildByName("uiRoot").getChildByName("dianjiSpine").getComponent(sp.Skeleton)
        this.dianjiSpine.node.active = false;

        main.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            GameController.instance.onCLickEvent(event)
        }, main.node);


        cc.debug.setDisplayStats(false);
        NativeSdkHelper.init()
        ServerConfig.init()
        AccountModel.instance.init()
        SettingModel.instance.init()
        RemoteAssetManager.instance.init()

    }

    public onCLickEvent(event: cc.Event.EventTouch) {
        let globalP: cc.Vec2 = event.getLocation()
        let tx: number = globalP.x - cc.winSize.width / 2;
        let ty: number = globalP.y - cc.winSize.height / 2;
        GameController.instance.playDianji(cc.v2(tx, ty))
    }

    public playDianji(p: cc.Vec2) {
        if (SettingModel.instance.effect) {
            this.dianjiSpine.node.active = true;
            this.dianjiSpine.node.setPosition(p);
            let animationName: string = 'dianji'
            let track = this.dianjiSpine.setAnimation(1, animationName, false);
            this.dianjiSpine.setTrackCompleteListener(track, (event) => {
                this.dianjiSpine.node.active = false;
            })
        }
    }

    //重新开始游戏
    public restartGame() {
        GameSocket.getInstance().closeSocket(false)
        UIManager.instance.closeAllView();
        cc.audioEngine.stopAll();
        cc.game.restart()
    }

    public gotoLoading() {
        UIManager.instance.closeAllView();
        GameSocket.getInstance().closeSocket(false)
        cc.audioEngine.stopAll();
        UIManager.instance.showView(ViewConfig.viewConfig.ViewLoading)
    }

    public switchAccount() {
        AccountModel.instance.account = ""
        AccountModel.instance.token = ""
        AccountModel.instance.password = ""
        AccountModel.instance.nickname = ""
        AccountModel.instance.save()
        this.gotoLogin()
    }

    public gotoLogin() {
        UIManager.instance.closeAllView();
        GameSocket.getInstance().closeSocket(false)
        UIManager.instance.showView(ViewConfig.viewConfig.ViewLogin)
        UIManager.instance.hideLoadingIcon()
    }

    public reInitGame(){
        Logger.log("gamecontroller.reInitGame===")
        GameResult.instance = null
     
    
    }

    public gotoHall(hadLogin: boolean) {
        RoomModel.instance.isPlayingGame = false
        this.reInitGame()
        if (!hadLogin) {
            ServerConfig.setHallServerSocket()
            GameSocket.getInstance().closeSocket(false)
            SocketClient.login(true)
        }
        UIManager.instance.closeAllView()
        UIManager.instance.showView(ViewConfig.viewConfig.ViewHall)
    }

    public gotoClub(isFromGameEnd) {
        RoomModel.instance.isPlayingGame = false
        // UserModel.instance.getNowClub(RoomModel.instance.clubid)
        // Logger.log("gotoClub=====", isFromGameEnd, UserModel.instance.clubList, UserModel.instance.nowClub)
        // if (isFromGameEnd) {
        //     ServerConfig.setHallServerSocket()
        //     GameSocket.getInstance().closeSocket(false)
        //     SocketClient.login(false, () => {
        //         UserModel.instance.getNowClub(RoomModel.instance.clubid)
        //         Logger.log("gotoClub===222==", isFromGameEnd, UserModel.instance.clubList, UserModel.instance.nowClub)
        //         UIManager.instance.closeAllView()
        //         if (UserModel.instance.nowClub) {
        //             UIManager.instance.showView(ViewConfig.viewConfig.ViewClubHall)
        //         } else {
        //             UIManager.instance.showView(ViewConfig.viewConfig.ViewClubList)
        //         }
        //     })
        // } else {
        //     UIManager.instance.closeAllView()
        //     if (UserModel.instance.nowClub) {
        //         UIManager.instance.showView(ViewConfig.viewConfig.ViewClubHall)
        //     } else {
        //         UIManager.instance.showView(ViewConfig.viewConfig.ViewClubList)
        //     }
        // }
    }

    public gotoRoom() {
        UIManager.instance.closeAllView();
        Logger.log("gotoRoom==", RoomModel.instance)
        let renshu: number = RoomModel.instance.conf.renshu
        let uiConfig: UI_Config
      
    }

    public playVideo(videoInfo: VideoInfo) {
        RoomModel.instance.isPlayVideo = true
        RoomModel.instance.videoInfo = videoInfo;
        // UIManager.instance.closeAllView();
        Logger.log("playVideo=", videoInfo)
        RoomModel.instance.gameType = videoInfo.gameResult.gameType
        RoomModel.instance.room_id = RoomModel.instance.videoInfo.gameResult.room_id
        RoomModel.instance.num_of_turns = RoomModel.instance.videoInfo.ju
        RoomModel.instance.conf = RoomModel.instance.videoInfo.gameResult.gameConf as RoomConfig
        RoomModel.instance.roomType =  RoomModel.instance.conf["roomType"]
        RoomModel.instance.playerList = videoInfo.playerList;
        RoomModel.instance.watchChairId = 0
        Logger.log("playVideo=222",  RoomModel.instance.num_of_turns, RoomModel.instance.conf)
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i]
            if (player.userid == UserModel.instance.userId) {
                RoomModel.instance.watchChairId = i
            }
        }
        let renshu: number = RoomModel.instance.conf.renshu
        let uiConfig: UI_Config
       
    }

    public leaveVideo() {
        RoomModel.instance.isPlayVideo = false
        let videoInfo: VideoInfo = RoomModel.instance.videoInfo
        // UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewApplyDismiss)
     
        // if (RoomModel.instance.gameType == GameConfig.Game_Type.ddz) {
        //     UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewDdzGame2);
        //     UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewDdzGame3);
        //     UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewDdzResult);
        // }
        // else if (RoomModel.instance.gameType == GameConfig.Game_Type.huanghua_mj) {
        //     UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewHuanghua_mjGame)
        //     UIManager.instance.closeSingleView(ViewConfig.viewConfig.ViewHuanghua_mjResult);
        // }
        RoomModel.instance.videoInfo = null;
    }
}